import config
import math

CONFIG = config.Config()

"""
    工具包
"""


def debug(msg, context=''):
    """
    打印调试信息
    :param msg: 信息
    :param context: 提示信息
    """
    if CONFIG.is_debug():
        print(f"== {context}: ", end='')
        if type(msg) == list:
            msg = ','.join(list(map(str, msg)))
        print(msg)


def warning(msg, context=''):
    """
    打印警告信息
    :param msg: 信息
    :param context: 提示信息
    """
    print(f"== {context}: ", end='')
    print(f"!!! warn !!! {msg}")


# 从网上查到的计算经纬度坐标两点间距离的算法，经验证是直线距离，误差较小

def dict_to_obj(cls, _dict: dict):
    obj = cls()
    for k, v in _dict.items():
        setattr(obj, k, v)
    return obj


def oi(a):
    return math.pi * a / 180


def td(a, b, c, d):
    return 6370996.81 * math.acos(math.sin(c) * math.sin(d) + math.cos(c) * math.cos(d) * math.cos(b - a))


def ew(a, b, c):
    if a > c:
        a -= c - b
    elif a < b:
        a += c - b
    return a


def lw(a, b, c):
    a = max(a, b)
    a = min(a, c)
    return a


def get_distance(p1, p2):
    '''
    计算两坐标之间距离
    :param p1: 坐标1（longitude,latitude）
    :param p2: 坐标2（longitude,latitude）
    :return: 距离
    '''
    x1, y1 = p1
    x2, y2 = p2

    a_lng = ew(x1, -180, 180)
    a_lat = lw(y1, -74, 74)
    b_lng = ew(x2, -180, 180)
    b_lat = lw(y2, -74, 74)

    return td(oi(a_lng), oi(b_lng), oi(a_lat), oi(b_lat))


